<?php
/**
 * Created by PhpStorm.
 * User: mt
 * Date: 2018/6/4
 * Time: 18:50
 */

namespace App\Handlers;
use App\Models\Student;
use GuzzleHttp\Client;

class FacePhotoHandler
{

    public function addFaceToCloud(Student $student)
    {
        $image_url = $student->photo;
        if (!$image_url){
            return ['status' => false];
        }
        $client = new Client();
        $res = $client->request('POST',
            'https://cloudapi.linkface.cn/search/image/insert',
            [
                'multipart' => [
                    [
                        'name' => 'api_id',
                        'contents' => env('FACE_API_ID')
                    ],
                    [
                        'name' => 'api_secret',
                        'contents' => env('FACE_API_SECRET')
                    ],
                    [
                        'name' => 'name',
                        'contents' => env('FACE_NAME')
                    ],
                    [
                        'name' => 'selfie_file',
                        'contents' => fopen(public_path().'/uploads/'.$image_url, 'r')
                    ]
                ],
                'verify' => false,
            ]);
        $body = $res->getBody();
        $data = json_decode($body, true);
        if($data['status'] == 'OK'){
            $token = $data['person_uuid'];
            $student->face_token = $token;
            $student->is_scaned = 1;
            $student->save();
            return ['status' => true];
        }

        return ['status' => false];
    }
}